var imgScaleRatioX = 300/475;
var imgScaleRatioY = 250/395;

/**
 * @author Stephan Pfaff (webmaster@derdaumen.net)
 */

function Item(type, basename, path, prefix){
	try {
		this.type = type;
		this.basename = basename;
		this.path = path;
		this.prefix = prefix;
		this.thumb_delete = false;
	} catch (e) {
		if(debug) alert('Initialization of new Item failed, missing or wrong '
				+'parameter! ('+e+')');
	}
};
Item.prototype.type = "";
Item.prototype.basename = "";
Item.prototype.path = "";
Item.prototype.prefix = "";
Item.prototype.thumb_delete = false;

Item.prototype.encodeToJSON = function(){
	return JSON.stringify(this);
};
Item.prototype.select = function(event) {
	this.show();
};
/**
 * Loads the dedicated children of the item
 */
Item.prototype.loadChilds = function() {};
/**
 * Shows item properties in the corresponding form elements.
 */
Item.prototype.show = function(){
	try {
		for (key in this) {
			switch (key) {
			case 'thumb':
				if (this[key] != null && this[key] != '') {
					$('#'+this.prefix+'_'+key).attr('src',
							this[key]+'?'+Math.floor(Math.random()*1001));
					$('#'+this.prefix+'_'+key).attr('alt', this[key]);
					$('#'+this.prefix+'_'+key).attr('title', this[key]);
				} else {
					$('#'+this.prefix+'_'+key).attr('src', 
										noImage+'?'
										+Math.floor(Math.random()*1001));
					$('#'+this.prefix+'_'+key).attr('alt', 'Kein Bild vorhanden.');
					$('#'+this.prefix+'_'+key).attr('title', 'Kein Bild vorhanden.');
				}
				$('#'+this.prefix+'_thumb_path').val(this[key]);
				break;
			case 'img':
				if (this[key] != null && this[key] != '') {
					var width = Math.abs(Math.round(
							(this.content_width*80-5)*imgScaleRatioX));
					var height = Math.abs(Math.round(
							(this.content_height*80-5)*imgScaleRatioY));
					var marginLeft = Math.abs(Math.round(
							(this.content_x*80)*imgScaleRatioX));
					var marginTop = Math.abs(Math.round(
							(this.content_y*80)*imgScaleRatioY));
					
					$('#'+this.prefix+'_'+key)
							.attr('width', width)
							.attr('height', height)
							.css({
								marginLeft: marginLeft+'px',
								marginTop: marginTop+'px'
							})
							.attr('src',
							this[key]+'?'+Math.floor(Math.random()*1001));
					$('#'+this.prefix+'_'+key).attr('alt', this[key]);
					$('#'+this.prefix+'_'+key).attr('title', this[key]);
				} else {
					$('#'+this.prefix+'_'+key).attr('src', '');
					$('#'+this.prefix+'_'+key).attr('alt', '');
					$('#'+this.prefix+'_'+key).attr('title', '');
				}
				$('#'+this.prefix+'_img_path').val(this[key]);
				break;
			case 'interactive':
				$('#'+this.prefix+'_'+key).val(Number(this[key]));
				break;
			default:
				if($('#'+this.prefix+'_'+key).length > 0) {
					switch (typeof this[key]) {
						case 'string':
						case 'number':
							$('#'+this.prefix+'_'+key).val(this[key]);
							break;
						case 'boolean':
							$('#'+this.prefix+'_'+key).attr('checked', this[key]);
							break;
					}
				}
			}
		}
		if(this.type == 'button') {
			$('#btn_btn_dummy').css({
				width: Math.abs(Math.round(
						73*imgScaleRatioX)),
				height: Math.abs(Math.round(
						73*imgScaleRatioY)),
				marginLeft: Math.abs(Math.round(
						(this.x*80)*imgScaleRatioX)),
				marginTop: Math.abs(Math.round(
						(this.y*80)*imgScaleRatioY))
			}).show();
			if(this.img == '') {
				$('#btn_content_dummy').css({
					marginLeft: Math.abs(Math.round(
							(this.content_x*80)*imgScaleRatioX)),
					marginTop: Math.abs(Math.round(
							(this.content_y*80)*imgScaleRatioY)),
					width: Math.abs(Math.round(
							(this.content_width*80-7)*imgScaleRatioX)),
					height: Math.abs(Math.round(
							(this.content_height*80-7)*imgScaleRatioY))
			}).show();
			} else {
				$('#btn_content_dummy').hide();
			}
		}
		if(this.type == 'category' && this.id == 0) {
			$('#btn_y').append('<option value="5">6</option>');
			$('#btn_content_y').append('<option value="5">6</option>');
			$('#btn_content_height').append('<option value="6">6</option>');
		} else if(this.type != 'button') {
			$('#btn_y option[value=5]').remove();
			$('#btn_content_y option[value=5]').remove();
			$('#btn_content_height option[value=6]').remove();
		}
		this.check();
	} catch (e) {
		if(debug) alert('show of ' + key + ' failed, error: ' + e);
	}
	this.loadChilds();
};
Item.prototype.assignValues = function(){
	try {
		for(key in this) {
			switch (key) {
				case 'thumb':
					this[key] = $('#'+this.prefix+'_'+key+'_path').val();
					break;
				case 'img':
					this[key] = $('#'+this.prefix+'_'+key+'_path').val();
					break;
				case 'interactive':
					this[key] = $('#'+this.prefix+'_'+key)
							.val();
					break;
				default:
					if($('#'+this.prefix+'_'+key).length > 0) {
						switch (typeof this[key]) {
							case 'number':
								this[key] = parseInt($('#'+this.prefix+'_'+key)
														.val());
								break;
							case 'string':
								this[key] = $('#'+this.prefix+'_'+key).val();
								break;
							case 'boolean':
								this[key] = $('#'+this.prefix+'_'+key)
												.is(':checked');
								break;
						}
					}
			}
		}
		return true;
	} catch (e) {
		if(debug) alert('assign of ' + key + ' failed, error: ' +e);
		return false;
	}
};
/**
 * Checks if all the mandatory inputs for a updating/saving an item are correct. 
 */
Item.prototype.check = function() {};
Item.prototype.resetItem = function(){
	this.show();
};